package logic

import (
	"context"
	"errors"
	"shuqi-novel/model/mysql"

	"shuqi-novel/shuqirpc/internal/svc"
	"shuqi-novel/shuqirpc/shuqirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *shuqirpc.AddRequest) (*shuqirpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.Book
	err := user.BookList(mysql.DB, in.Name)
	if err == nil {
		return nil, errors.New("用户已存在")
	}
	data := &mysql.Book{
		Name:    in.Name,
		Title:   in.Title,
		Context: in.Context,
		UserId:  user.UserId,
	}

	err = data.Addbook(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &shuqirpc.AddResponse{
		BookId: int64(data.ID),
	}, nil
}
